/**
 * @author: Loan
 * Last modified by Huynh on April 25
 * 
 * TODO What to be done:
 * -> Login to the server.
 * -> Find an opponent, set up a game.
 * -> Store game configuration: is this the first or second player.
 * 		[
 * 			Use a variable named gameType to achieve this. The value of gameType
 * 			is Const.ONLINE_HOST or Const.ONLINE_GUEST
 * 		]
 * -> Start a new game activity.
 * 		[
 * 			If value of gameType is set, then this is a simple step.
 * 			One can do this:
 * 				Intent game = new Intent(this, GameDisplay.class);
				game.putExtra("game_type", gameType); //Note, this is an important line!
				startActivity(game);
 * 		]
 * These tasks can be implemented in a couple of files if needed and
 * they are independent of game logic.
 * 
 */
package controller;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.dbhelper.AccountDbHelper;
import android.fourrows.R;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Login extends Activity{
	private Button mLoginBtn;
	private Button mCancelBtn;
	private EditText mUsername;
	private EditText mPassword;
	//check valid username and password
	private boolean checkLoginInput(String username, String password) {
		// TODO Auto-generated method stub
		
		if( username.length() < 6 || password.length() < 6)
		{
			Log.i("LOGIN","too short username or password");
			createDialog("LOGIN","Username and password should contain at least 6 character"); 
			return false;
		}
		String newUsername = username.replaceAll("\\s","");
		String newPwd = password.replaceAll("\\s","");
		//Log.i("LOGIN",newUsername+"__"+newUsername.length() +" "+ username+"_"+ username.length() +" "+  newPwd.length() +" "+  password.length());
		if (newUsername.length() < username.length() || newPwd.length() < password.length())
		{
			//Log.i("LOGIN",newUsername.length() +" "+ username.length() +" "+  newPwd.length() +" "+  password.length());
			createDialog("LOGIN","Username and password should not contain whitespace"); 
			return false;
		}
		return true;
		
	}
	//create an anonymous implementation of OnClickListenerr
	private OnClickListener myButton = new OnClickListener(){
		public void onClick(View v){
			switch(v.getId()){
			case R.id.login_ok_button:
				Log.i("LOGIN","Hit ok button");
				String username = mUsername.getText().toString(); // get username.
				String password = mPassword.getText().toString(); // get password.
				checkLoginInput(username,password);
				//do login to the system
				AccountDbHelper login = new AccountDbHelper(username,password);
				String result = login.actionLogin();
				
				
				if (! result.equals("__failed__")) 
				{
					Intent game_intent = new Intent(getApplicationContext(), FourRows.class);
					startActivity(game_intent);
				}else{
					createDialog("LOGIN","wrong username and password should"); 
				}
				
				break;
			case R.id.login_cancel_button:
				Log.i("LOGIN","Hit cancel button");
				finish();
				break;
			default: break;
			}
		}

	};
	protected void createDialog(String title, String text) {
		AlertDialog ad = new AlertDialog.Builder(this)
				.setPositiveButton("Ok", null).setTitle(title).setMessage(text)
				.create();

		ad.show();
	}
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		Log.i("Login", "Login activity created");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.login);
		mUsername = (EditText) findViewById(R.id.et_login_username);
		mPassword = (EditText) findViewById(R.id.et_login_password);
		mLoginBtn = (Button) findViewById(R.id.login_ok_button);
		mLoginBtn.setOnClickListener(myButton);
		mCancelBtn = (Button)findViewById(R.id.login_cancel_button);
		mCancelBtn.setOnClickListener(myButton);
	}

}
